Modul:Codename
-------------------------------------------------------------------------------- -- This module calls information from releases and series by their Codename. -- Functions are invoked for use in various templates and for generation of -- site CSS. -- See for details on how they are used. -------------------------------------------------------------------------------- local Codename = {} local getArgs = require('Dev:Arguments').getArgs local userError = require('Dev:User error') local printTable = require('Module:Table').tostring local uoTables = mw.loadData('Module:Codename/data') -------------------------------------------------------------------------------- -- Helper functions -------------------------------------------------------------------------------- -- For a dictionary t and list f, returns t[f1][f2][f...][f#f] or nil if -- any of those intermediate values are missing. -- -- @example -- dive({foo = {bar = {baz = 'hi'}}}, {'foo', 'bar', 'baz'}) --> 'hi' -- -- @TODO -- Move to Module:Table or something? It might be useful elsewhere... local function dive(t, f) for _, value in ipairs(f) do if type(t) ~= 'table' then return nil end t = tvalue end return t end -------------------------------------------------------------------------------- -- Database queries -------------------------------------------------------------------------------- Codename.tables = {} Codename.tables.odr = {} for key, value in pairs(uoTables) do Codename.tableskey = {} Codename.tables.odrkey = {} -- For use in modules for index, value2 in ipairs(value) do local id = value2.id -- Convert a list of tables with ids to a dictionary, where each key is -- an id, and each value is its corresponding table. -- -- ex. --> {a={id='a'}, b={id='b'}} Codename.tableskeyid = value2 -- Convert a list of tables with ids to a list of ids. -- -- ex. --> {'a', 'b'} table.insert(Codename.tables.odrkey, id) end -- For use in wikitext via #invoke Codenamekey = function (frame) local args = getArgs(frame) local value = dive(Codename.tableskey, args) if type(value) 'table' then return printTable(value) end return value end end -------------------------------------------------------------------------------- -- Sideicons ( ) -------------------------------------------------------------------------------- -- 1 = Rel codename -- style' = Style for small lines -- style' = Style for normal lines -- -- Size is position-agnostic, so any line can be small- or standard-sized. function Codename._relicontext(args) local cname = args1 local data = (Codename.tables.relcname or {}).sideicon local icontext = mw.html.create() if not data then error('invalid codename') end for i, position in ipairs{'first', 'second'} do local line = data.. 'line' local size = data.. 'size' or 'standard' local style = args.. ' style' if line then if i > 1 then icontext:tag('br') end icontext:tag(style and 'span' or '') :wikitext(line) :cssText(style or '') end end return icontext end function Codename.relicontext(frame) local args = getArgs(frame) local success, response = pcall(Codename._relicontext, args) if not success then return userError('invalid codename. See for more information') end return response end -- Generates a containing everything to be added to Article Information. function Codename._artinf(cname) local data = Codename.tables.relcname if not data then error('invalid codename') end local firstLine = data.sideicon.firstline local secondLine = data.sideicon.secondline local firstClass = 'two-line' local secondClass = 'two-line-small' if not secondLine then firstClass = 'one-line' secondClass = '' elseif data.sideicon.firstsize 'small' then firstClass, secondClass = secondClass, firstClass end local span = mw.html.create('span') :addClass('page-info-icon') :addClass(cname) :addClass('a') :tag('span') :addClass('text-container') :tag('span') :addClass(firstClass) :wikitext(firstLine) :done() :tag(secondLine and 'span' or '') :addClass(secondClass) :wikitext(secondLine or '') :done() :done() return mw.html.create('span') :addClass('titleicon') :wikitext('' .. tostring(span) .. '') end function Codename.artinf(frame) local args = getArgs(frame) local success, response = pcall(Codename._artinf, args1) if not success then return userError('invalid codename. See for more information') end return response end -------------------------------------------------------------------------------- -- Generators for lists (for use on , Project:Scope, etc.) -------------------------------------------------------------------------------- -- Generates an ordered list of media. -- arg 'rel' or 'ser'. -- optional info allows it to display some info from /data. function Codename._generator(dset, info) local list = mw.html.create('ul') local data = Codename.tablesdset local odr = Codename.tables.odrdset local function addItem(node, elem, info) if not elem then return 'null' end local addedinfo = '' if info then addedinfo = ': (' .. eleminfo .. ')' end return node:tag('li') :wikitext('[[' .. elem.link .. "|" .. elem.name .. "'']]" .. addedinfo) end for _, parent in ipairs(odr) do if not dataparent.sub then local item = addItem(list, dataparent, info) local sublist for _, child in ipairs(odr) do if datachild.sub parent then if not sublist then sublist = item:tag('ul') end addItem(sublist, datachild, info) end end end end return list end function Codename.generator(frame) local args = getArgs(frame) return Codename._generator(args1, args2) end -- Generate tables of all class colors function Codename.ctbl() local container = mw.html.create('div') :css('display', 'flex') :css('font-family', 'monospace') :css('text-align', 'center') local namedtbls = { ser = 'Series', rel = 'Releases' } for _, dset in ipairs{'ser', 'rel'} do local odr = Codename.tables.odrdset local tbl = container:tag('table') :addClass('table') :addClass('darktext') :addClass('mw-collapsible') :addClass('mw-collapsed') :attr('data-expandtext', 'Show ' .. #odr .. ' items') :attr('data-collapsetext', 'Hide ' .. #odr .. ' items') :css('width', '50%') :tag('tr') :css('background', '#F5F5F5') :css('font-style', 'italic') :tag('th') :attr('colspan', '7') :css('text-align', 'left') :wikitext(namedtblsdset) :done() :done() :tag('tr') :css('background', '#CCC') :tag('th') :attr('scope', 'col') :wikitext('Codename') :done() :tag('th') :attr('scope', 'col') :wikitext('Header') :done() :tag('th') :attr('scope', 'col') :wikitext('Subheader') :done() :done() for _, cname in ipairs(odr) do tbl:tag('tr') :tag('th') :attr('scope', 'row') :css('background', '#CCC') :css('font-style', 'italic') :wikitext('[.. Codename.tables[dsetcname.link .. '|' .. cname .. ']]') :done() :tag('td') :addClass(cname .. 'a') :wikitext(cname .. 'a') :done() :tag('td') :addClass(cname .. 'b') :wikitext(cname .. 'b') :done() end end return container end return Codename --